package com.example.demo.security.filter;

import com.example.demo.security.authentication.CustomUsernamePasswordAuthentication;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * describe:
 *
 * @author lianying
 * @date 2020/12/06
 */
public class CustomUsernamePasswordFilter extends AbstractAuthenticationProcessingFilter {

    public CustomUsernamePasswordFilter(AuthenticationManager authenticationManager){
        super("/customLogin",authenticationManager);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
        System.out.println("-----------进入了自定义认证过滤器");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        CustomUsernamePasswordAuthentication authentication=new CustomUsernamePasswordAuthentication();
        authentication.setUsername(username);
        authentication.setPassword(password);
        getAuthenticationManager().authenticate(authentication);
        return authentication;
    }
}
